using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;
using Petra.Gis.DataServices.WKT.Support;

namespace Petra.Gis.DataServices.WKT
{
  public partial class GeometryFactory
  {
    /***********************************************
     * 28.8.2007
     * 
     * creates MultiPolygon
     * from wkt string
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/
    private static GeometryBase ToMultiPolygon(ref string inputStr, System.Type parentGeomType)
    {

      RegexGeometryParser rgp = new RegexGeometryParser(WKTGeometryType.MultiPolygon);
      RegexGeometry rg = rgp.ParseGeometry(ref inputStr);

      MultiArea oGeom = new MultiArea();

      foreach (RegexGeometry tmpRegexGeom in rg)
      {

        Polygon tmpGeom = ToPolygon(tmpRegexGeom, WKTGeometryType.MultiPolygon);
        oGeom.Add(tmpGeom);
      }

      return oGeom;
    }






    /***********************************************
     * 28.8.2007
     * 
     * creates wkt string 
     * from Polygon
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/

    private static string MultiPolygonToString(GeometryBase geometry)
    {

      string sep = "";

      //cast to Polygon first
      MultiArea _mg = (MultiArea)geometry;

      System.IO.StringWriter mStrWriter = new System.IO.StringWriter();


      mStrWriter.Write(getWktName[WKTGeometryType.MultiPolygon]);
      mStrWriter.Write("(");

      Polygon tmpItem;
      foreach (GeometryBase g in _mg)
      {
        //cast GeometryBase to LineRing
        tmpItem = (Polygon)g; //safety, raises exception if CompositePolygon inside !!
        mStrWriter.Write(sep);
        //mStrWriter.Write("(");
        mStrWriter.Write(PolygonToString(tmpItem, WKTGeometryType.MultiPolygon));
        //mStrWriter.Write(")");
        sep = ",";
      }
      mStrWriter.Write(")");

      return mStrWriter.ToString();
    }
  }
}
